<?
    define ('XLS_ALIGN_LEFT', 1);
    define ('XLS_ALIGN_CENTER', 2);
    define ('XLS_ALIGN_RIGHT', 3);
    define ('XLS_ALIGN_FILL', 4);
    define ('XLS_FSHADE' , 0x80);
    define ('XLS_FBOTTOM' , 0x40);
    define ('XLS_FTOP' , 0x20);
    define ('XLS_FRIGHT' , 0x10);
    define ('XLS_FLEFT' , 0x08);
    define ('XLS_FALL' , 0x78);

	class Excel {
	    var $xls_data = "";
	    var $error = "";
	    var $download = "true";
	    var $nCurRow = 0;
	    var $nCurCol = 0;

	    function NewLine( ) {
	    	$this->nCurRow++;
	    	$this->nCurCol = 0;
	    }

	    function WriteNext ( $Value , $bNewLine = 0, $nColSpan = 1) {
	    	if ( is_numeric( $Value ) || $Value === '' ) {
	    		$this->WriteNumber( $this->nCurRow, $this->nCurCol, $Value ) ;
	    	} else {
	    		$this->WriteLabel( $this->nCurRow, $this->nCurCol, $Value ) ;
	    	}
	    	$this->nCurCol++;

	    	// Pone los Espacios Vacios que necesita para el ColSpan
	        for ( $nIndex = 1 ; $nIndex < $nColSpan ; $nIndex++ ) {
	            $this->WriteLabel( $this->nCurRow, $this->nCurCol, '' ) ;
                $this->nCurCol++;
    	    }

	    	if ( $bNewLine == 1 ) {
		    	$this->nCurRow++;
		    	$this->nCurCol = 0;
	    	}

	    	return $Value ;
	    }

	   /**
	    * Constructor, writting begin of  Excel file (binary).
	    *
	    * @param $download boolean  true, if ask for "Save/Open", false -
	    *    if open Excel (application/x-exel).
	    */

	    function Excel($download = "true") {
	        $this->download = $download;
	        $this->Begin();
	    }

	   /**
	       * Excel begin of file header
	    * Send the header to client
	    *
	    * @param $filename string - name of file, for save
	    * (actually, if $download = true)
	    */

	    function _Header($filename) {
	        header ("Expires: 0");
	        header ("Last-Modified: " . gmdate("D,d M YH:i:s") . " GMT");
	        header ("Cache-Control: no-cache, must-revalidate");
	        header ("Pragma: no-cache");
	        if ($this->download){
	            header ("Content-type: application/x-msexcel");
	            //header("Content-Type: application/vnd.ms-excel");
				header ("Content-Disposition: attachment; filename=$filename");
	        } else {
	            header ("Content-type: application/msexcel");
	        }

	        header ("Content-Description: PHP Generated Data" );
	    }

	   /**
	       * Begin of Excel File.
	    *
	    * @private
	    */

	    function Begin() {
	        $str = pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
	        $this->xls_data = $str;
	        return $str;
	    }

	   /**
	    * End of Excel File (binary)
	    *
	    * @private
	    */

	    function EOF() {
	        $str = pack("ss", 0x0A, 0x00);
	        $this->xls_data .=  $str;
	        return $str;
	    }

	   /**
	    * Function for writting number (double) into row $Row,
	    * and column $Col.
	    *
	    * @param $Row integer - Row
	    * @param $Col integer - Column
	    * @param $Value number - value
	    *
	    */

	    function WriteNumber($Row, $Col, $Value) {
	        $str = pack("sssss", 0x203, 14, $Row, $Col, 0x0);
	        $str .= pack("d", $Value);
	        $this->xls_data .= $str;
	        return $str;
	    }


	   /**
	    * Function for writting label(string) into row $Row,
	    * and column $Col.
	    * @param $Row integer - Row
	    * @param $Col integer - Column
	    * @param $Value number - value
	    *
	    */

	    function WriteLabel($Row, $Col, $Value ) {
	        $L = strlen($Value);
	        $str = pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
	        $str .= $Value;
	        $this->xls_data .=  $str;
	        return $str;
	    }

	   /**
	    * Function to send file to client(browser)
	    *
	    * @param $filename string -  name of file (actually, if $download = true)
	    *
	    */

	    function SendFile($filename = "test.xls") {
	        $this->_Header($filename);
	        echo $this->xls_data;
	        echo $this->EOF();
	    }

	   /**
	    * Function to get Excel (binary) data
	    *
	    * @param $eof boolean - id true, returnet data will with the end of Excel File (bin)
	    *
	    */

	    function GetData($eof = "true") {
	        if ($eof) return $xls_data . $this->EOF();
	        else return $xls_data;
	    }


	   /**
	    * Function to write Excel-data to $file
	    *
	    * @param $efile string - File name.
	    *
	    */
	    function toFile($file = "test.xls") {
	        $fp = @fopen($file,"w");
	        if (is_resource($fp)) {
	            fwrite($fp, $this->xls_data);
	            fwrite($fp, $this->EOF());	            
	            fclose($fp);
	            return true;
	        } else {
	            addMessage( "No se puede acceder al archivo '$file'" );
	            return false;
	        }
	    }


	   /**
	    * Destructor
	    * Unset The Excel Object.
	    *
	    * @private
	    *
	    */

	    function dExcel()
	    {
		    unset($this->xls_data);
		    unset($this);
	    }


        /*
        COLWIDTH record - column width (type = 36)
          Offset  Name    Size Contents
          ------  ----    ---- --------
          4   colFirst 1  first column in the range
          5   colLast  1  last column in the range
          6   dx   2  column width

          Description
           The COLWIDTH record sets the column width for a range of
           columns specified by colFirst and colLast.  The dx field is
           an unsigned integer specifying the column width in units of
           1/260 of a character.
        */
        function ColWidth($ColDesde, $ColHasta, $Value )
        {
	        $str = pack("ssccs", 36, 4, $ColDesde, $ColHasta, $Value * 260);
	        $this->xls_data .=  $str;
	        return $str;
        }

        /*
        COLUMN DEFAULT record - default cell attributes (type = 32)
          Offset  Name    Size Contents
          ------  ----    ---- --------
          4   colMic   2  first column that has a default
                  cell
          6   colMac   2  last column that has a default
                  cell, plus 1
          8   rgrgbAttr   var  array of default cell attributes
        */
        function ColumnDefault( $ColDesde, $ColHasta, $Atributo)
        {

        }

	    function WriteLabelAttr($Row, $Col, $Value, $Align=0 ) {
	        $L = strlen($Value);
	        $str = pack("sssscccc", 0x004, 8 + $L, $Row, $Col, 0x0 , 0x16, $Align, $L);
	        $str .= $Value;
	        $this->xls_data .=  $str;
	        return $str;
	    }


/* XF 0243h*/
        function XF($Value )
        {
	        $str = pack("ssccs", 0x243, 4, $ColDesde, $ColHasta, $Value * 260);
	        $this->xls_data .=  $str;
	        return $str;
        }


	} // End of class Excel.

?>
